﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;
using CrystalWind.Example.ExcelDna.CustomerExtentions;
using ExcelDna.Registration;
using ExcelDna.Integration.CustomUI;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace CrystalWind.Example.ExcelDna
{
    [ComVisible(true)]
    public class ExampleRibbon: ExcelRibbon
    {
        /// <summary>
        /// ribbon的xml文件必需作为嵌入资源。不然用户重新打开excel的时候，不会调用GetRibbonXml方法。
        /// 原因暂时没搞清楚。
        /// </summary>
        /// <param name="xmlName"></param>
        /// <returns></returns>
        static public string GetRibbonXml(string xmlName)
        {
            return AppGlobals.GetResourceContent("CrystalWind.Example.ExcelDna." + xmlName);

            //return File.ReadAllText(xmlName);
        }

        /// <summary>
        /// 返回功能区的xml内容.可在vsto项目中的Ribbon做好设计,右键"导出xml",直接复用
        /// </summary>
        /// <param name="RibbonID"></param>
        /// <returns></returns>
        public override string GetCustomUI(string RibbonID)
        {
            return GetRibbonXml("TestRibbon.xml");
        }

    

        /// <summary>
        /// 可以使用不同的方法名字
        /// </summary>
        /// <param name="control"></param>
        public void OnButtonPressed(IRibbonControl control)
        {
            var app = ExcelDnaUtil.Application as Excel.Application;
            app.OnUndo("cshi", "EmptyRibbon_Undo");
            System.Windows.Forms.MessageBox.Show("可以点击撤销");
        }


        /// <summary>
        /// 必须使用固定方法签名.参考:https://msdn.microsoft.com/en-us/library/aa722523.aspx
        /// 方法名字与ribbon的xml定义头上的onLoad属性值一致即可。
        /// </summary>
        /// <param name="ribbon"></param>
        public void OnLoad(IRibbonUI ribbon)
        {
            
            System.Windows.Forms.MessageBox.Show("OnLoad");
        }

        
    }


    /// <summary>
    /// ExcelRibbon的子类,里面的静态方法都当做是宏
    /// 可以在pia库中被调用.例如Application.Run或Application.Onundo设置
    /// </summary>
    public class EmptyRibbon: ExampleRibbon
    {
        public static void EmptyRibbon_Undo()
        {
            System.Windows.Forms.MessageBox.Show("EmptyRibbon_Undo");
        }
    }

}
